#include <ximera/system/application.hpp>
#include <ximera/logger.hpp>
#include <ximera/xml/xmldocument.hpp>
#include <ximera/xml/xmliterator.hpp>
#include <ximera/xml/xmlelement.hpp>

#include <iostream>

struct AppTestHandler : ximera::system::AppHandler
{
	virtual void onStart(ximera::system::AppEventArgs& args)
    {
        try
        {
            testScripting(args.app);
        }
        catch (ximera::Exception& e)
        {
            args.app.shouldClose(true);
            XIMERA_LOG() << e << std::endl;
        }
    }

private:
    void testScripting(ximera::system::Application& app)
    {
        using namespace ximera;
        SharedPtr<scripting::IVirtualMachine> vm(app.scriptingSystem()->createVirtualMachine());
        SharedPtr<scripting::IScript> script(app.scriptingSystem()->createScript());

        script->data(L"vec1 = ximera.math.Vector3(10, 3, 2); \n"
            L"vec2 = ximera.math.Vector3(1, 2, 0); \n"
            L"print(vec1 * vec2);");

        vm->run(script);

        vm->run(L"print('hello'); vec3 = ximera.math.Vector3(10, 3, 2); \n"
            L"vec4 = ximera.math.Vector3(1, 2, 0); \n"
            L"print('hola'); \n"
            L"vec5 = vec3 + vec4; \n"
            L"print('hola'); \n"
            L"vec4.y = 0; \n"
            L"print(vec5 * vec4); \n");
        //L"print(vec3:at(1));");
        //L"print(vec5:length());");

        vm->run(
            L"doc = ximera.xml.XmlDocument() \n"
            L"doc:loadXml('<?xml version=\"1.0\" encoding=\"utf-8\"?><test></test>') \n"
            L"print('done')"
        );
    }
};

int main()
{
    using namespace ximera;

    try
    {
        XIMERA_LOG().stream(&std::wcout);

        system::AppSettings settings;
        settings[L"ximera.plugins.scripting"] = XIMERA_PLUGIN(L"XimeraLua");

        system::Application app(settings);

        AppTestHandler handler;
        app.addHandler(handler);

        return app.run();
    }
    catch (ximera::Exception& e)
    {
        XIMERA_LOG() << e << std::endl;
    }
}
